1_
XSLT file frame
XML <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <!-- content --> </xsl:stylesheet>
2_
Reference from XML to XSL
XML <?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet href="myStyle.xsl" type="text/xsl"?> <people> <!-- content --> </people>This does not work by default in Chrome. You have to start Chrome with "--allow-file-access-from-files" http://stackoverflow.com/questions/2981524/how-can-i-make-xslt-work-in-chrome
3_
Output all XML data
XML <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" encoding="utf-8"/> <xsl:template match="/"> <html> <body> <xsl:apply-templates/> </body> </html> </xsl:template> </xsl:stylesheet>
4_
Modify output of one element
XML <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" encoding="utf-8"/> <xsl:template match="/"> <html> <body> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="department"> <b><xsl:value-of select="."/></b><br/> </xsl:template> </xsl:stylesheet>
5_
How to embed xslt template in xml file
File encoding must be UTF-8 in order to work in Google Chrome.
XML <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="#stylesheet"?> <!DOCTYPE persons [ <!ATTLIST xsl:stylesheet id ID #REQUIRED> ]> <persons> <xsl:stylesheet version="1.0" id="stylesheet" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <!-- skip transfornimg stylesheet elements --> <xsl:template match="xsl:stylesheet" /> <xsl:template match="/"> <html> <body> Works in Chrome (v55) only <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="person"> <h1>Person</h1> First name: <xsl:value-of select="@firstName" /> Last name: <xsl:value-of select="@lastName" /> </xsl:template> </xsl:stylesheet> <!-- data --> <person firstName="John" lastName="First" /> <person firstName="Paul" lastName="Second" /> </persons>
6_
Using xsl:if
XML <xsl:template match="person"> <xsl:if test="department='IT'> <xsl:value-of select="department"/> <xsl:if/> </xsl:template>
7_
Using xsl:choose
XML <xsl:template match="person"> <xsl:choose> <xsl:when test="department='IT'"> <i><xsl:apply-templates /></i> </xsl:when> <xsl:otherwise> <xsl:apply-templates /> </xsl:otherwise> </xsl:choose> </xsl:template>
8_
List all persons in sorted order
XML <xsl:template match="/"> <html> <body> <xsl:for-each select="//person"> <xsl:sort select="@id"/> <xsl:value-of select="lastName"/> </xsl:for-each> </body> </html> </xsl:template>
9_
Call template as procedure
XML <!-- caller --> <xsl:call-template name="CreateLink"> <xsl:with-param name="reference" select="http://mysite" /> <xsl:with-param name="title" select="Go to my site."/> </xsl:call-template> <!-- "procedure" --> <xsl:template name="CreateLink"> <xsl:param name="reference" /> <xsl:param name="title" /> <xsl:text disable-output-escaping="yes"><a href="</xsl:text> <xsl:value-of select="$reference" /> <xsl:text disable-output-escaping="yes">">/xsl:text> <xsl:value-of select="$title" /> <xsl:text disable-output-escaping="yes"</a></xsl:text> </xsl:template>